<?php

namespace Cktadmin\Controller;

use Common\Controller\BasicAdmin;
use Common\Service\DataService;

class NumberController extends BasicAdmin {

    public $table = 'courier_num';

    public function index() {
        $this->title = '快递单号';
        $where = array('type' => 2);
        $get = I('get.');
        (isset($get['key']) && $get['key'] !== '') && $where['coding|state|coding_status'] = array('like', "%{$get['key']}%");
        $count = M($this->table)->where($where)->count();
        $db = M($this->table)->where($where)->order('add_time desc');
        return parent::_list($db, $count);
    }

    public function add() {
        return parent::_form($this->table, 'form');
    }

    public function edit() {
        return parent::_form($this->table, 'form');
    }

    protected function _add_form_filter(&$vo) {
        if (IS_POST) {
            $vo['add_time'] = time();
            $vo['internet_time'] = strtotime($vo['internet_time']);
        } else {
            $vo['internet_time'] = date('Y-m-d H:i:s', time());
        }
    }

    protected function _edit_form_filter(&$vo) {
        if (IS_POST) {
            $vo['internet_time'] = strtotime($vo['internet_time']);
        } else {
            if ($vo['internet_time'] > 0) {
                $vo['internet_time'] = date('Y-m-d H:i:s', $vo['internet_time']);
            } else {
                $vo['internet_time'] = date('Y-m-d H:i:s', time());
//                $vo['internet_time'] = '';
            }
        }
    }

    public function del() {
        if (DataService::update($this->table)) {
            $this->success('删除成功！');
        }
        $this->error('删除失败！');
    }

    public function upload() {
        $upload = new \Think\Upload(); // 实例化上传类
        $upload->maxSize = 1024000; // 设置附件上传大小
        $upload->exts = array('xls', 'xlsx'); // 设置附件上传类型
        $upload->rootPath = './static';
        $upload->savePath = '/tmp/';
        $upload->subName = '';
        $file_info = $upload->uploadOne($_FILES['file']);
        $path = $upload->rootPath . $file_info['savepath'];
        $filename = $file_info['savename'];
        $exts = $file_info['ext'];
        if (!$file_info) { //上传错误提示
            $this->error($upload->getError());
        } else {
            $this->import_excel($path, $filename, $exts);
        }
    }

    //导入excel文件
    public function import_excel($path, $filename, $exts = 'xls') {
        $file = $path . $filename;
        if ($exts == 'xls') {
            $PHPReader = new \PHPExcel_Reader_Excel5();
        } else if ($exts == 'xlsx') {
            $PHPReader = new \PHPExcel_Reader_Excel2007();
        }
        //载入文件
        $PHPExcel = $PHPReader->load($file);
        //获取表中的第一个工作表，如果要获取第二个，把0改为1，依次类推
        $currentSheet = $PHPExcel->getSheet(0);
        $allColumn = $currentSheet->getHighestColumn();  //获取总列数
//        if ($allColumn != "F") {
//            $this->error('导入失败，请检查excel表格格式是否正确');
//        }
        $allRow = $currentSheet->getHighestRow();  //获取总行数
        //获取数据存入数组
        $data = array();
        for ($Row = 2; $Row <= $allRow; $Row++) {
            if ($currentSheet->getCell('A' . $Row)->getValue() != '') {
                $data[] = array(
                    'coding' => $currentSheet->getCell('A' . $Row)->getValue(),
                    'state' => $currentSheet->getCell('B' . $Row)->getValue(),
                    'category' => $currentSheet->getCell('C' . $Row)->getValue(),
                    'price' => $currentSheet->getCell('D' . $Row)->getValue(),
                    'internet_time' => 0,
                    'coding_status' => $currentSheet->getCell('F' . $Row)->getValue(),
                    'add_time' => time()
                );
                if ($currentSheet->getCell('E' . $Row)->getValue()) {
                    $data[$Row - 2]['internet_time'] = strtotime(gmdate('Y-m-d H:i:s', \PHPExcel_Shared_Date::ExcelToPHP($currentSheet->getCell('E' . $Row)->getValue())));
                }
            }
        }
        //删除上传后的excel，节省空间
        if (is_file($file)) {
            unlink($file);
        }
        $result = M('courier_num')->addAll($data);
        if ($result > 0) {
            $this->success('导入成功！');
        } else {
            $this->error('导入失败！');
        }
    }

}
